<template>
  <div class="register-container">
    <div class="register-card neu-card">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>创建您的账号</p>
      </div>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="registerForm.username" 
            class="neu-input" 
            placeholder="请输入用户名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="name">姓名</label>
          <input 
            type="text" 
            id="name" 
            v-model="registerForm.name" 
            class="neu-input" 
            placeholder="请输入姓名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input 
            type="email" 
            id="email" 
            v-model="registerForm.email" 
            class="neu-input" 
            placeholder="请输入邮箱"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="registerForm.password" 
            class="neu-input" 
            placeholder="请输入密码"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="registerForm.confirmPassword" 
            class="neu-input" 
            placeholder="请再次输入密码"
            required
          />
        </div>
        
        <div class="form-actions">
          <button type="submit" class="neu-button primary" :disabled="loading">
            {{ loading ? '注册中...' : '注册' }}
          </button>
          <router-link to="/login" class="login-link">
            已有账号？返回登录
          </router-link>
        </div>
      </form>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userService } from '../api/services'

const router = useRouter()

// 注册表单数据
const registerForm = ref({
  username: '',
  name: '',
  email: '',
  password: '',
  confirmPassword: ''
})

// 状态变量
const loading = ref(false)
const errorMessage = ref('')

// 注册处理
const handleRegister = async () => {
  // 表单验证
  if (registerForm.value.password !== registerForm.value.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return
  }
  
  try {
    loading.value = true
    errorMessage.value = ''
    
    // 准备提交的数据
    const userData = {
      username: registerForm.value.username,
      name: registerForm.value.name,
      email: registerForm.value.email,
      password: registerForm.value.password
    }
    
    const response = await userService.register(userData)
    
    if (response.code === 200) {
      // 注册成功，跳转到登录页
      router.push({
        path: '/login',
        query: { registered: 'success' }
      })
    } else {
      errorMessage.value = response.message || '注册失败'
    }
  } catch (error) {
    console.error('注册错误:', error)
    errorMessage.value = '注册失败，请稍后再试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
@import '../assets/styles/neumorphism.css';

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background-color: var(--bg-color);
}

.register-card {
  width: 100%;
  max-width: 450px;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h2 {
  color: var(--primary-blue);
  margin-bottom: 10px;
}

.register-header p {
  color: var(--text-secondary);
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-weight: 500;
  color: var(--text-secondary);
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-top: 10px;
}

.login-link {
  text-align: center;
  color: var(--primary-green);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.login-link:hover {
  color: var(--primary-green-dark);
}

.error-message {
  margin-top: 20px;
  padding: 10px;
  border-radius: 8px;
  background-color: rgba(244, 67, 54, 0.1);
  color: var(--danger);
  text-align: center;
}
</style>